package com.liming.product.tast;

import com.liming.auth.client.UserAuthClient;
import com.liming.common.enums.OperationTypeEnum;
import com.liming.common.pojo.entity.product.Product;
import com.liming.product.service.ProductMircoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liming
 * @date 2025/4/8
 */
@Slf4j
@Component
@AllArgsConstructor
public class TaskService {

    private final UserAuthClient userAuthClient;
    private final ProductMircoService productMircoService;

    @Scheduled(cron = "0 43 11 * * ?")
    public void stagnantProductCount() {
        log.info("定时任务 - 计算每个商品的分数");
        Map<Long, List<Integer>> map = userAuthClient.productIdActivityTypeListMap();
        List<Product> updateList = new ArrayList<>();
        map.forEach((productId, operationTypeList) -> {
            Product update = new Product();
            Double score = 0.0;
            for (Integer operationType : operationTypeList) {
                Double rate = OperationTypeEnum.getRateByCode(operationType);
                score += rate;
            }
            update.setId(productId);
            update.setActivityScore(score);
            updateList.add(update);
        });
        // 更新分数
        productMircoService.updateBatchById(updateList);
    }
}
